package hapi.db;

#if php
import php.db.Object;
#elseif neko
import neko.db.Object;
#end

class GenericObjectsPage<T:Object> implements hapi.mvc.DisplayablePage {
	
	var items:List<T>;
    var pageIndex:Int;
	var pagesCount:Int;
	
	public function new(items:List<T>, pageIndex:Int, pagesCount:Int) {
		this.items = items;
		this.pageIndex = pageIndex;
		this.pagesCount = pagesCount;
	}
	
	public function displayOn(view:hapi.mvc.View) {
		view.assign("items", items);
		view.assign("has_items", (items.length > 0) ? "true" : "false");
		view.assign("page_number", getIndex() );
		view.assign("pages_count", getPagesTotalCount() );
		view.assign("is_last", isLast());
		view.assign("is_first", isFirst());
		view.assign("pages", pagesIndexes());
	}
	
	function pagesIndexes() {
		var a = new Array();
		for ( i in 1...(getPagesTotalCount()+1)) {
			a.push( { page:i } );
		}
		return a;
	}
	
	public function isLast():Bool {
		return pageIndex >= pagesCount;
	}
	
	public function isFirst():Bool {
		return pageIndex <= 1;
	}
	
	public function getPagesTotalCount():Int {
		return pagesCount;
	}
	
	public function getIndex():Int {
		return pageIndex;
	}
}